/*
 * =====================================================================================
 *
 *       Filename:  observer.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  02/12/2011 02:11:40 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  dp (8023), dp8023@gmail.com
 *        Company:  
 *
 * =====================================================================================
 */

#ifndef _OBSERVER_H_
#define _OBSERVER_H_

#include <iostream>
#include <string>
#include <set>

using namespace std;

class subject;

class observer
{
	public:
		observer(){}
		virtual ~observer(){}

		virtual void update(subject *psub, void *parg = NULL) = 0;
};

class subject
{
	public:
		subject():m_bchanged(false){}
		virtual ~subject(){}

		void attach(observer *pobs);
		void detach(observer *pobs);
		void detachall();
		void notify(void *parg = NULL);
		bool haschanged();
		int getobservercount();

	protected:
		void setchanged();
		void clearchanged();
		
	private:
		bool m_bchanged;
		set<observer *> m_setobs;
};

class bloger:public subject
{
	public:
		bloger(){}
		~bloger(){}

		void publish(const string &strcontent);
};

class portal:public subject
{
	public:
		portal(){}
		~portal(){}

		void publish(const string &strcontent);
};

class rssreader:public observer
{
	public:
		rssreader(const string &strname):m_strname(strname){}
		~rssreader(){}
		
		void update(subject *psub, void *parg = NULL);

	private:
		string m_strname;
};

class mailreader:public observer
{
	public:
		mailreader(const string &strname):m_strname(strname){}
		~mailreader(){}

		void update(subject *psub, void *parg = NULL);

	private:
		string m_strname;
};

#endif

